package com.googlecode.cswish.security;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * @author Jerry.Feng Date: 2004-10-25 18:28:05
 * @version $id: $
 */
public class PermissionFilter implements Filter {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger
            .getLogger(PermissionFilter.class);    	    
    
    /* 
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub       
    }

    /* 
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub        
    }

    /* 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	boolean pass = true;
        if (request instanceof HttpServletRequest) {
            logger.info(((HttpServletRequest)request).getRequestURI() + "?formAction=" + request.getParameter("formAction"));
            
     		pass = SafeManager.getInstance().check((HttpServletRequest)request, 
				(HttpServletResponse)response);
        }
        
        if (pass) {
        	chain.doFilter(request, response);
        }
    }
}
